|
1
|
|
|
/* |
|
2
|
|
|
* Copyright (c) 2017-2018 Rafael da Silva Rocha. |
|
3
|
|
|
* |
|
4
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
|
5
|
|
|
* a copy of this software and associated documentation files (the |
|
6
|
|
|
* "Software"), to deal in the Software without restriction, including |
|
7
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
|
8
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
|
9
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
|
10
|
|
|
* the following conditions: |
|
11
|
|
|
* |
|
12
|
|
|
* The above copyright notice and this permission notice shall be |
|
13
|
|
|
* included in all copies or substantial portions of the Software. |
|
14
|
|
|
* |
|
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
16
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
17
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
18
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
|
19
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|
20
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
|
21
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @fileoverview The byte-data API. |
|
27
|
|
|
* @see https://github.com/rochars/byte-data |
|
28
|
|
|
*/ |
|
29
|
|
|
|
|
30
|
|
|
/** @module byteData */ |
|
31
|
|
|
|
|
32
|
|
|
import endianness from './lib/endianness.js'; |
|
33
|
|
|
import {reader_, setUp_, writeBytes_} from './lib/packer.js'; |
|
34
|
|
|
import {validateNotUndefined} from './lib/validation.js'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Read a string of UTF-8 characters from a byte buffer. |
|
38
|
|
|
* @see https://encoding.spec.whatwg.org/#the-encoding |
|
39
|
|
|
* @see https://stackoverflow.com/a/34926911 |
|
40
|
|
|
* @param {!Uint8Array|!Array<!number>} buffer A byte buffer. |
|
41
|
|
|
* @param {number=} index The index to read. |
|
42
|
|
|
* @param {?number=} len The number of bytes to read. |
|
43
|
|
|
* @return {string} |
|
44
|
|
|
* @throws {Error} If read a value that is not UTF-8. |
|
45
|
|
|
*/ |
|
46
|
|
|
export function unpackString(buffer, index=0, len=null) { |
|
47
|
|
|
len = len !== null ? index + len : buffer.length; |
|
48
|
|
|
/** @type {string} */ |
|
49
|
|
|
let str = ""; |
|
50
|
|
|
while(index < len) { |
|
51
|
|
|
/** @type {number} */ |
|
52
|
|
|
let charCode = buffer[index++]; |
|
53
|
|
|
if (charCode >> 7 == 0) { |
|
|
|
|
|
|
54
|
|
|
str += String.fromCharCode(charCode); |
|
55
|
|
|
} else { |
|
56
|
|
|
/** @type {number} */ |
|
57
|
|
|
let count = 0; |
|
58
|
|
|
if (charCode >> 5 == 0x06) { |
|
59
|
|
|
count = 1; |
|
60
|
|
|
} else if (charCode >> 4 == 0x0e) { |
|
61
|
|
|
count = 2; |
|
62
|
|
|
} else if (charCode >> 3 == 0x1e) { |
|
63
|
|
|
count = 3; |
|
64
|
|
|
} |
|
65
|
|
|
charCode = charCode & (1 << (8 - count - 1)) - 1; |
|
66
|
|
|
for (let i = 0; i < count; i++) { |
|
67
|
|
|
charCode = (charCode << 6) | (buffer[index++] & 0x3f); |
|
68
|
|
|
} |
|
69
|
|
|
if (charCode <= 0xffff) { |
|
70
|
|
|
str += String.fromCharCode(charCode); |
|
71
|
|
|
} else { |
|
72
|
|
|
charCode -= 0x10000; |
|
73
|
|
|
str += String.fromCharCode( |
|
74
|
|
|
((charCode >> 10) & 0x3ff) + 0xd800, |
|
75
|
|
|
(charCode & 0x3ff) + 0xdc00); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
return str; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Write a string of UTF-8 characters as a byte buffer. |
|
84
|
|
|
* @see https://encoding.spec.whatwg.org/#utf-8-encoder |
|
85
|
|
|
* @param {string} str The string to pack. |
|
86
|
|
|
* @return {!Array<number>} The next index to write on the buffer. |
|
87
|
|
|
* @throws {Error} If a character in the string is not UTF-8. |
|
88
|
|
|
*/ |
|
89
|
|
|
export function packString(str) { |
|
90
|
|
|
/** @type {!Array<!number>} */ |
|
91
|
|
|
let bytes = []; |
|
92
|
|
|
for (let i = 0; i < str.length; i++) { |
|
93
|
|
|
/** @type {number} */ |
|
94
|
|
|
let codePoint = str.codePointAt(i); |
|
95
|
|
|
if (codePoint < 128) { |
|
96
|
|
|
bytes.push(codePoint); |
|
97
|
|
|
} else { |
|
98
|
|
|
/** @type {number} */ |
|
99
|
|
|
let count = 0; |
|
100
|
|
|
/** @type {number} */ |
|
101
|
|
|
let offset = 0; |
|
102
|
|
|
if (codePoint <= 0x07FF) { |
|
103
|
|
|
count = 1; |
|
104
|
|
|
offset = 0xC0; |
|
105
|
|
|
} else if(codePoint <= 0xFFFF) { |
|
106
|
|
|
count = 2; |
|
107
|
|
|
offset = 0xE0; |
|
108
|
|
|
} else if(codePoint <= 0x10FFFF) { |
|
109
|
|
|
count = 3; |
|
110
|
|
|
offset = 0xF0; |
|
111
|
|
|
i++; |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
bytes.push((codePoint >> (6 * count)) + offset); |
|
114
|
|
|
while (count > 0) { |
|
115
|
|
|
bytes.push(0x80 | (codePoint >> (6 * (count - 1)) & 0x3F)); |
|
116
|
|
|
count--; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
return bytes; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Write a string of UTF-8 characters to a byte buffer. |
|
125
|
|
|
* @param {string} str The string to pack. |
|
126
|
|
|
* @param {!Uint8Array|!Array<number>} buffer The output buffer. |
|
127
|
|
|
* @param {number=} index The index to write in the buffer. |
|
128
|
|
|
* @return {number} The next index to write in the buffer. |
|
129
|
|
|
* @throws {Error} If a character in the string is not valid ASCII. |
|
130
|
|
|
*/ |
|
131
|
|
|
export function packStringTo(str, buffer, index=0) { |
|
132
|
|
|
/** @type {!Array<!number>} */ |
|
133
|
|
|
let bytes = packString(str); |
|
134
|
|
|
for (let i = 0; i < bytes.length; i++) { |
|
135
|
|
|
buffer[index++] = bytes[i]; |
|
136
|
|
|
} |
|
137
|
|
|
return index; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
// Numbers |
|
141
|
|
|
/** |
|
142
|
|
|
* Pack a number as a byte buffer. |
|
143
|
|
|
* @param {number} value The number. |
|
144
|
|
|
* @param {!Object} theType The type definition. |
|
145
|
|
|
* @return {!Array<number>} The packed value. |
|
146
|
|
|
* @throws {Error} If the type definition is not valid. |
|
147
|
|
|
* @throws {Error} If the value is not valid. |
|
148
|
|
|
*/ |
|
149
|
|
|
export function pack(value, theType) { |
|
150
|
|
|
/** @type {!Array<!number>} */ |
|
151
|
|
|
let output = []; |
|
152
|
|
|
packTo(value, theType, output); |
|
153
|
|
|
return output; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Pack an array of numbers as a byte buffer. |
|
158
|
|
|
* @param {!Array<number>|!TypedArray} values The values. |
|
159
|
|
|
* @param {!Object} theType The type definition. |
|
160
|
|
|
* @return {!Array<number>} The packed values. |
|
161
|
|
|
* @throws {Error} If the type definition is not valid. |
|
162
|
|
|
* @throws {Error} If any of the values are not valid. |
|
163
|
|
|
*/ |
|
164
|
|
|
export function packArray(values, theType) { |
|
165
|
|
|
/** @type {!Array<!number>} */ |
|
166
|
|
|
let output = []; |
|
167
|
|
|
packArrayTo(values, theType, output); |
|
168
|
|
|
return output; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Pack a number to a byte buffer. |
|
173
|
|
|
* @param {number} value The value. |
|
174
|
|
|
* @param {!Object} theType The type definition. |
|
175
|
|
|
* @param {!Uint8Array|!Array<number>} buffer The output buffer. |
|
176
|
|
|
* @param {number=} index The index to write. |
|
177
|
|
|
* @return {number} The next index to write. |
|
178
|
|
|
* @throws {Error} If the type definition is not valid. |
|
179
|
|
|
* @throws {Error} If the value is not valid. |
|
180
|
|
|
*/ |
|
181
|
|
|
export function packTo(value, theType, buffer, index=0) { |
|
182
|
|
|
setUp_(theType); |
|
183
|
|
|
return writeBytes_(value, |
|
184
|
|
|
theType, |
|
185
|
|
|
buffer, |
|
186
|
|
|
index, |
|
187
|
|
|
index + theType.offset, |
|
188
|
|
|
validateNotUndefined, |
|
189
|
|
|
theType.be); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Pack a array of numbers to a byte buffer. |
|
194
|
|
|
* @param {!Array<number>|!TypedArray} values The value. |
|
195
|
|
|
* @param {!Object} theType The type definition. |
|
196
|
|
|
* @param {!Uint8Array|!Array<number>} buffer The output buffer. |
|
197
|
|
|
* @param {number=} index The buffer index to write. |
|
198
|
|
|
* @return {number} The next index to write. |
|
199
|
|
|
* @throws {Error} If the type definition is not valid. |
|
200
|
|
|
* @throws {Error} If the value is not valid. |
|
201
|
|
|
*/ |
|
202
|
|
|
export function packArrayTo(values, theType, buffer, index=0) { |
|
203
|
|
|
setUp_(theType); |
|
204
|
|
|
for (let i=0; i < values.length; i++) { |
|
205
|
|
|
index = writeBytes_( |
|
206
|
|
|
values[i], |
|
207
|
|
|
theType, |
|
208
|
|
|
buffer, |
|
209
|
|
|
index, |
|
210
|
|
|
index + theType.offset, |
|
211
|
|
|
validateNotUndefined, |
|
212
|
|
|
theType.be); |
|
213
|
|
|
} |
|
214
|
|
|
return index; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Unpack a number from a byte buffer. |
|
219
|
|
|
* @param {!Uint8Array|!Array<!number>} buffer The byte buffer. |
|
220
|
|
|
* @param {!Object} theType The type definition. |
|
221
|
|
|
* @param {number=} index The buffer index to read. |
|
222
|
|
|
* @return {number} |
|
223
|
|
|
* @throws {Error} If the type definition is not valid |
|
224
|
|
|
*/ |
|
225
|
|
|
export function unpack(buffer, theType, index=0) { |
|
226
|
|
|
setUp_(theType); |
|
227
|
|
|
if (theType.be) { |
|
228
|
|
|
endianness(buffer, theType.offset, index, index + theType.offset); |
|
229
|
|
|
} |
|
230
|
|
|
/** @type {number} */ |
|
231
|
|
|
let value = reader_(buffer, index); |
|
232
|
|
|
if (theType.be) { |
|
233
|
|
|
endianness(buffer, theType.offset, index, index + theType.offset); |
|
234
|
|
|
} |
|
235
|
|
|
return value; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Unpack an array of numbers from a byte buffer. |
|
240
|
|
|
* @param {!Uint8Array|!Array<!number>} buffer The byte buffer. |
|
241
|
|
|
* @param {!Object} theType The type definition. |
|
242
|
|
|
* @param {number=} index The start index. Assumes 0. |
|
243
|
|
|
* @param {?number=} end The end index. Assumes the buffer length. |
|
244
|
|
|
* @return {!Array<number>} |
|
245
|
|
|
* @throws {Error} If the type definition is not valid |
|
246
|
|
|
*/ |
|
247
|
|
|
export function unpackArray(buffer, theType, index=0, end=buffer.length) { |
|
248
|
|
|
/** @type {!Array<!number>} */ |
|
249
|
|
|
let output = []; |
|
250
|
|
|
unpackArrayTo(buffer, theType, output, index, end); |
|
251
|
|
|
return output; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* Unpack a array of numbers to a typed array. |
|
256
|
|
|
* @param {!Uint8Array|!Array<!number>} buffer The byte buffer. |
|
257
|
|
|
* @param {!Object} theType The type definition. |
|
258
|
|
|
* @param {!TypedArray|!Array<!number>} output The output array. |
|
259
|
|
|
* @param {number=} index The start index. Assumes 0. |
|
260
|
|
|
* @param {?number=} end The end index. Assumes the buffer length. |
|
261
|
|
|
* @throws {Error} If the type definition is not valid |
|
262
|
|
|
*/ |
|
263
|
|
|
export function unpackArrayTo(buffer, theType, output, index=0, end=buffer.length) { |
|
264
|
|
|
setUp_(theType); |
|
265
|
|
|
while ((end - index) % theType.offset) { |
|
266
|
|
|
end--; |
|
267
|
|
|
} |
|
268
|
|
|
if (theType.be) { |
|
269
|
|
|
endianness(buffer, theType.offset); |
|
270
|
|
|
} |
|
271
|
|
|
for (let i = 0; index < end; index += theType.offset, i++) { |
|
|
|
|
|
|
272
|
|
|
output[i] = reader_(buffer, index); |
|
273
|
|
|
} |
|
274
|
|
|
if (theType.be) { |
|
275
|
|
|
endianness(buffer, theType.offset); |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
|
|
|